﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool
{
    public partial class ReportLiefertmine : Form
    {
        private SQL sql;
        private ReportLiefertermineHelper helper;
        private Boolean isInit = true;

        public ReportLiefertmine()
        {
            InitializeComponent();
            this.Text = "Liefertermine: " + Login.currentDBName;
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new ReportLiefertermineHelper(reportViewer1, this);

            fillElementsInit();

            fillTablesInit();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            try
            {
                String[,] liefer = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM vw_rep_Lieferterminliste", "SELECT Liefertermin FROM vw_rep_Lieferterminliste ORDER BY Liefertermin");
                comboBox1.Items.Clear();
                comboBox1.DropDownHeight = 300;
                comboBox1.Items.Add("");
                for (int a = 0; a < liefer.GetLength(1); a++)
                {
                    comboBox1.Items.Add(liefer[0, a].Substring(0, 10));
                }
                comboBox1.Refresh();
                comboBox1.Text = "";

                String[,] schiff = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM vw_rep_LieferterminlisteSchiffIst", "SELECT Expr1 FROM vw_rep_LieferterminlisteSchiffIst ORDER BY Expr1 ASC");
                comboBox2.Items.Clear();
                comboBox2.DropDownHeight = 300;
                comboBox2.Items.Add("");
                for (int a = 0; a < schiff.GetLength(1); a++)
                {
                    comboBox2.Items.Add(schiff[0, a].Substring(0, 10));
                }
                comboBox2.Refresh();
                comboBox2.Text = "";
            }
            catch (Exception e)
            {

            }
        }
        #endregion

        #region Filter
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                try
                {
                    DateTime test = Convert.ToDateTime(comboBox1.Text);
                    String date = test.Year + "-" + test.Month + "-" + test.Day;
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("lieferungSchiffSoll LIKE '%{0}%'", comboBox1.Text);
                    this.reportViewer1.RefreshReport();
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                try
                {
                    DateTime test = Convert.ToDateTime(comboBox2.Text);
                    String date = test.Year + "-" + test.Month + "-" + test.Day;
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Liefertermin LIKE '%{0}%'", comboBox2.Text);
                    this.reportViewer1.RefreshReport();
                }
                catch (Exception ex)
                {
                }
            }
        }
        #endregion

        #region SollBereitstellungHH bei Export/Druck blockieren
        private void reportViewer1_Print(object sender, Microsoft.Reporting.WinForms.ReportPrintEventArgs e)
        {
            blockSollBereitstellungHH();
        }

        private void reportViewer1_ReportExport(object sender, Microsoft.Reporting.WinForms.ReportExportEventArgs e)
        {
            blockSollBereitstellungHH();
        }

        private void blockSollBereitstellungHH()
        {
            if (MessageBox.Show("Sollen diese Artikel als kommissioniert gekennzeichnet werden? Die Spalte \"Soll Bereitstellung HH\" im Modul \"DeliveryChain\" wird dann für die ausgewählten Artikel für die weitere Bearbeitung gesperrt.", "Bestätigung", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (helper.getTable1() != null) 
                {
                    DataView view = helper.getTable1().DefaultView;
                    DataTable table = view.ToTable();

                    for (int a = 0; a < table.Rows.Count; a++)
                    {
                        sql.sendSQL("UPDATE tbl_materialDetailExtended SET Kommissioniert = 1 WHERE ID ="+table.Rows[a]["ID"].ToString());
                    }
                }
            }
        }
        #endregion
    }
}
